#include "user_cfg.h"

/**
*\*\name   RCC_MCO_Pin_Config.
*\*\brief  Selects HSI as System clock source and configure HCLK, PCLK
*\*\         prescalers.
*\*\parm  src:
*\*\        - SYSCLK_SRC_HSI48M
*\*\        - SYSCLK_SRC_HSI40M
*\*\parm  freq(Hz):
*\*\        - 48000000
*\*\        - 40000000
*\*\        - 24000000
*\*\        - 8000000
**/
void SetSysClock(uint8_t src, uint32_t freq)
{
    uint32_t status_value = 0, counter_value = 0;
    uint32_t sys_div = 0;


    /* Set Latency before switching the clock to prevent the chip flying  */
    FLASH_Latency_Set(FLASH_LATENCY_1);
    switch (freq)
    {
      case 8000000:
        if(src == SYSCLK_SRC_HSI40M)
        {
            sys_div = RCC_HSICLK_DIV5;
        }
        else
        {
            sys_div = RCC_HSICLK_DIV6;
        }
        break;
      case 24000000:
        sys_div = RCC_HSICLK_DIV2;
        break;
      case 40000000:
        sys_div = RCC_HSICLK_DIV1;
        break;
      case 48000000:
        sys_div = RCC_HSICLK_DIV1;
        break;
      default:
        while(1);

    }

    /* HCLK = SYSCLK */
    RCC_Hclk_Config(RCC_SYSCLK_DIV1);
    /* PCLK = HCLK */
    RCC_Pclk_Config(RCC_HCLK_DIV1);

    if(src == SYSCLK_SRC_HSI48M)
	{

        /* Select HSI 48M*/
        PWR->CTRL3 &= ~PWR_CTRL3_HSISEL;	//48M

        /* Wait till HSI is ready and if Time out is reached exit */
        do
        {
                status_value = RCC->HSICTRL & RCC_HSICTRL_HSI48MRDF;
                counter_value++;
        } while ((status_value == 0) && (counter_value != HSI_STARTUP_TIMEOUT));

        if(status_value == RCC_HSICTRL_HSI48MRDF)
        {
             RCC_Sysclk_Config(sys_div);
        }
        else
        {
            while(1);
        }

	}
    else if(src == SYSCLK_SRC_HSI40M)
	{
        /* Select HSI 40M */
        PWR->CTRL3 |= PWR_CTRL3_HSISEL; //40M

        /* Wait till HSI is ready and if Time out is reached exit */
        do
        {
                status_value = RCC->HSICTRL & RCC_HSICTRL_HSI40MRDF;
                counter_value++;
        } while ((status_value == 0) && (counter_value != HSI_STARTUP_TIMEOUT));

        if(status_value == RCC_HSICTRL_HSI40MRDF)
        {
             RCC_Sysclk_Config(sys_div);

        }
        else
        {
            while(1);
        }
	 }
	 else
	 {
		while(1);
	 }

     /* Flash wait state 0: SYSCLK <= 24M 1: SYSCLK <= 48M */
    if(freq <= SYSCLK_FREQ_24M)
    {
        /*8M\24M */
        FLASH_Latency_Set(FLASH_LATENCY_0);
    }
    else
    {
        /*40M\48M */
        FLASH_Latency_Set(FLASH_LATENCY_1);
    }
}

void TIM1_Config_Irq(void)
{
    NVIC_InitType NVIC_InitStructure;
	TIM_TimeBaseInitType TIM_TimeBaseStructure;
	RCC_APB_Peripheral_Clock_Enable(RCC_APB_PERIPH_TIM1);
	TIM_Base_Struct_Initialize(&TIM_TimeBaseStructure);

	//设置中断优先级
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_UP_TRG_COM_IRQn;;
    NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Initializes(&NVIC_InitStructure);

	/* Time base configuration */
    TIM_TimeBaseStructure.Period    = 47; 					// freq = 48MHz/48 = 1MHz
    TIM_TimeBaseStructure.Prescaler = 999;					// 1ms   (psc + 1) = TIMER_CLK / Fpwm / (arr + 1)		Fpwm = 1 / 0.001 = 1000hz
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;
    TIM_Base_Initialize(TIM1, &TIM_TimeBaseStructure);

    /* Prescaler configuration */
    TIM_Base_Reload_Mode_Set(TIM1, TIM_PSC_RELOAD_MODE_IMMEDIATE);

    /* TIM1 enable update irq */
    TIM_Interrupt_Enable(TIM1, TIM_INT_UPDATE);

    /* TIM1 enable counter */
    TIM_On(TIM1);
}

void UART1_Config_Irq(void)
{
	
}
